﻿#include "widget_find.h"
#include "ui_widget_find.h"
#include "QDebug"

widget_find::widget_find(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::widget_find)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_QuitOnClose,false);
    setWindowFlags(Qt::WindowStaysOnTopHint);
    setWindowTitle(tr("查找"));
    setFixedSize(320,90);
    //设置默认焦点和按键
    ui->expString->setFocus();
    ui->Button_next->setDefault(true);

    installEventFilter(this);
}

widget_find::~widget_find()
{
    delete ui;
}

bool widget_find::eventFilter(QObject *, QEvent *event)
{
    if(QEvent::WindowDeactivate == event->type()){
        setWindowOpacity(0.5);
        return true ;
    }else if(QEvent::WindowActivate == event->type()){
        setWindowOpacity(1);
        return true;
    }
    return false;
}

void widget_find::on_Button_next_clicked()
{
    QTextDocument::FindFlags opt = 0;
    if(ui->checkBox_caseSensitively->isChecked()){
        opt |= QTextDocument::FindCaseSensitively;
    }
    if(ui->checkBox_wholeWords->isChecked()){
        opt |= QTextDocument::FindWholeWords;
    }
    emit sig_findString(ui->expString->text(), opt);
}

void widget_find::on_Button_previous_clicked()
{
    QTextDocument::FindFlags opt = QTextDocument::FindBackward;
    if(ui->checkBox_caseSensitively->isChecked()){
        opt |= QTextDocument::FindCaseSensitively;
    }
    if(ui->checkBox_wholeWords->isChecked()){
        opt |= QTextDocument::FindWholeWords;
    }
    emit sig_findString(ui->expString->text(), opt);
}

void widget_find::on_Buttoncancel_clicked()
{
    close();
}
